package com.rodteixo.Replicador;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class RemoteConection {
	
	private String host;
	private String username;
	private String password;
	private String dbName;
	private boolean isConnected = false;
	private Connection conn;
    private Statement Stat;
	
	public RemoteConection (String host, String user, String password, String dbName) {
		this.host = host;
		this.username = user;
		this.password = password;
		this.dbName = dbName;

		conecta();
	}
	
	public RemoteConection (String ODBCName, String user, String password) {
		this.username = user;
		this.password = password;
		this.dbName = ODBCName;

		conectaODBC();
	}
	
	
	private void conecta() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {
		    conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dbName + "?user=" + username + "&password=" + password);
		    System.out.println("RemoteConection - Conectado - OK");
		    Stat = conn.createStatement();
		    this.isConnected = true;

		} catch (SQLException ex) {
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
	}

	private void conectaODBC() {

		try {
			Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		try {
		    conn = DriverManager.getConnection("jdbc:odbc:" + host);
		    System.out.println("RemoteConection - Conectado - OK");
		    Stat = conn.createStatement();
		    this.isConnected = true;

		} catch (SQLException ex) {
			ex.printStackTrace();
		    // handle any errors
		    System.out.println("SQLException: " + ex.getMessage());
		    System.out.println("SQLState: " + ex.getSQLState());
		    System.out.println("VendorError: " + ex.getErrorCode());
		}
	}
	
	public void close() {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public ResultSet getConsulta(String Sql) {

		ResultSet RecordSet = null;
		if (this.isConnected) {
			try {
				RecordSet = Stat.executeQuery(Sql);
				System.out.println("RemoteConnection - Query executada - OK");
			} catch(SQLException sqlex) {
				sqlex.printStackTrace();
			}
		}

		return RecordSet;
	}

	public String achaCampo (String NomeTabela, String Campo, String Condicao) {

		String Result = "";
		ResultSet RecordSet = null;

		try {
			RecordSet = Stat.executeQuery("Select " + Campo + " From " + NomeTabela + " Where (" + Condicao + ");");
		} catch(SQLException sqlex) {
			sqlex.printStackTrace();
		}

		try {
			if (RecordSet.next()) {
				Result = RecordSet.getString(Campo);
			}
			RecordSet.close();
		} catch (SQLException e) {
			System.out.println("Erro ao acessar o campo");
			e.printStackTrace();
		}

		return Result;
	}

	public String achaCampo (String sql, String Campo) {

		String Result = "";
		ResultSet RecordSet = null;

		try {
			RecordSet = Stat.executeQuery(sql);
		} catch(SQLException sqlex) {
			sqlex.printStackTrace();
		}

		try {
			if (RecordSet.next()) {
				Result = RecordSet.getString(Campo);
			}
			RecordSet.close();
		} catch (SQLException e) {
			System.out.println("Erro ao acessar o campo");
			e.printStackTrace();
		}

		return Result;
	}
	
	public boolean ExecutaBD(String sql) {
		boolean result = false;
		try {
			Stat.execute(sql);
			result = true;
			System.out.println("RemoteConection - Comando Sql executado - OK");
		} catch(SQLException sqlex) {
			sqlex.printStackTrace();
		}
		return result;
	}
}